<template>
    <view>
        <view class="mx-3 mt-5">
            <table border="1" width="340px">
                <tr>
                    <th nowrap="nowrap">型号</th>
                    <th nowrap="nowrap">扫描数据</th>
                    <th nowrap="nowrap">删除按钮</th>
                </tr>
                <tr v-for="(item,index) of statistical" :key="index">
                    <td>{{item.xinghao}}</td>
                    <td>{{item.data.length}}</td>
                    <td>
                        <view class="btnx" @click="del(index)">删除</view>
                    </td>
                </tr>
            </table>
        </view>
        <view class="wrap" style="margin-top: 10px">
            <u-row gutter="16">
                <u-button type="success"  size="medium"  @click="statisticalReport">统计上报</u-button>
        
            </u-row>
        </view>
        <u-toast ref="uToast"/>
        <u-popup v-model="show" mode="center">
          <view style="
              color: white;
              background-color: #09690f;
              width: 100%;
              height: 100%;
              padding: 40rpx;
            ">
            <u-form :model="form" ref="uForm">
              <u-form-item label="申请人" prop="escort" :border-bottom="false">
                <u-input v-model="form.escort" type="select" @click="showEscort = true" />
                <u-select v-model="showEscort" mode="single-column" :list="escortList" @confirm="confirmEscort"></u-select>
              </u-form-item>
            </u-form>
            <u-button @click="submit">确定</u-button>
          </view>
        </u-popup>
    </view>
</template>

<script>
  const sdkwx = uni.requireNativePlugin('GGRfid-SdkWX');
  import sqlLite from '../../common/sqllite-utils.js';
  export default {
    data() {
      return {
        statistical:[],
        RFID:'',
        rfid_list:[],
        count:'',
        equipmentID:uni.getStorageSync('leixingId'),
        list:[],
        xinghao:'',
        liberyid:uni.getStorageSync('shiyouid'),
        //定义一个空对象来存储rfid的信息，从入库表获取
        data:{
          realcount:0
        },
        show:false,
        form: {
          escort: ''
        },
        showEscort:false,
        escortList:[],
        
      }
    },
    
    onReady() {
      this.$refs.uToast.show({
        title: '开启扫描成功',
        type: 'success',
      });
      const readerName = '/dev/ttysWK0:115200';
      const packageSize = 64;
      const freeWait = 100;
      const power = 22;
      
      sdkwx.open(readerName, packageSize, freeWait, power);
      const that = this;
      // 设置回调事件
      sdkwx.setTagEpcCallback((res) => {
        that.RFID = res.log.epc;
        
        //如果扫描到的rfid标签rfid数组里面没有的话
        if(!that.rfid_list.includes(that.RFID)) {
          const datas = {}
         
          //查询
          const tableName = 'Synchronoustables';
  
          
          sqlLite.selectFromTable(
             	`select * from ${tableName} where rfid = ${that.RFID}`,
          ).then(res => {
           datas.id = res[0].hb_rock_zhuangxh_id
           datas.xinghao = res[0].xin_hao
               that.list.push(datas)
               console.log(that.list)
             
             //定义一个map对象，dest空数组
             let map = {},
                 dest = [];
                 /**
                  * 循环遍历list数组
                  */
                 for(let i = 0; i < that.list.length; i++) {
                   const ai = that.list[i]
                   if(!map[ai.xinghao]) {
                     dest.push({
                         xinghao: ai.xinghao,
                         name: ai.name,
                         count:ai.count,
                         data: [ai]
                     });
                     map[ai.xinghao] = ai
                   }else {
                     for(let j = 0; j < dest.length; j++) {
                       const dj = dest[j]
                       if(dj.xinghao == ai.xinghao) {
                         dj.data.push(ai)
                         break
                       }
                     }
                   }
                 }
             that.statistical = dest
        
          }).catch(err => {
            console.log(err,'查找当前rfid信息错误的回调')
          })
          
          that.rfid_list.push(that.RFID)
        }
        
      });
    },
    created() {
      sqlLite.selectFromTable(
        `select * from 'accompanyingPersonTable'`,
      ).then(res => {
        res.forEach((v, k) => {
              this.escortList.push({
                value: v.ID,
                label: v.nickName
              })
            })
      })
    },
    onUnload() {
      sdkwx.close();
    },
    methods: {
      //删除
      del() {
        
      },
      /**
       * 统计上报
       */
      statisticalReport() {
        this.show = true
      },
      confirmEscort(e) {
        this.form.escort = e[0].label
        uni.setStorage({
          key: "countaccompanyingPerson",
          data: e[0].value,
        });
      },
      
      submit() {
        const kushi = uni.getStorageSync('shiyouid');
        const userid = uni.getStorageSync('countaccompanyingPerson');
        uni.setStorage({
          key: "rfidList",
          data: this.rfid_list,
        });
          console.log(uni.getStorageSync('rfidList'))
        this.show = false
      }
    }
  }
</script>

<style>
.mx-3 {
  color: #fff;
}
</style>

